package com.knowledgepoint.study.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.knowledgepoint.study.mapper.KnowledgePointCategoryLevelTwoMapper;
import com.knowledgepoint.study.model.entity.KnowledgePointCategoryLevelTwo;
import com.knowledgepoint.study.service.KnowledgePointCategoryLevelTwoService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 二级知识点分类 Service 实现
 */
@Service
public class KnowledgePointCategoryLevelTwoServiceImpl extends ServiceImpl<KnowledgePointCategoryLevelTwoMapper, KnowledgePointCategoryLevelTwo> implements KnowledgePointCategoryLevelTwoService {

    @Override
    public List<KnowledgePointCategoryLevelTwo> listAll() {
        LambdaQueryWrapper<KnowledgePointCategoryLevelTwo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(KnowledgePointCategoryLevelTwo::getSortOrder);
        return baseMapper.selectList(queryWrapper);
    }
    
    @Override
    public List<KnowledgePointCategoryLevelTwo> listByParentId(Integer parentId) {
        LambdaQueryWrapper<KnowledgePointCategoryLevelTwo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(KnowledgePointCategoryLevelTwo::getParentId, parentId)
                   .orderByAsc(KnowledgePointCategoryLevelTwo::getSortOrder);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public boolean isNameExist(String name, Integer parentId, Integer excludeId) {
        LambdaQueryWrapper<KnowledgePointCategoryLevelTwo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(KnowledgePointCategoryLevelTwo::getName, name)
                   .eq(KnowledgePointCategoryLevelTwo::getParentId, parentId);
        if (excludeId != null) {
            queryWrapper.ne(KnowledgePointCategoryLevelTwo::getId, excludeId);
        }
        return baseMapper.selectCount(queryWrapper) > 0;
    }
    
    @Override
    public <P extends IPage<KnowledgePointCategoryLevelTwo>> P page(P page, Wrapper<KnowledgePointCategoryLevelTwo> queryWrapper) {
        return baseMapper.selectPage(page, queryWrapper);
    }
    
    @Override
    public KnowledgePointCategoryLevelTwo getDetailById(Integer id) {
        return baseMapper.selectById(id);
    }
    
    @Override
    public boolean add(KnowledgePointCategoryLevelTwo category) {
        return save(category);
    }
    
    @Override
    public boolean update(KnowledgePointCategoryLevelTwo category) {
        return updateById(category);
    }
    
    @Override
    public boolean removeById(Integer id) {
        return super.removeById(id);
    }
}
